//
//  MemberManagementViewController.m
//  NuwaRobot
//
//  Created by Kendama on 2017/12/4.
//  Copyright © 2017年 Kendama. All rights reserved.
//

#import "MemberManagementViewController.h"
#import "MemberManagementHeaderView.h"
#import "MemberManagementTableViewCell.h"
#import "AddMemberViewController.h"
#import "GetUserListApi.h"
#import "DeleteUserApi.h"
#import "MemberModel.h"

@interface MemberManagementViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) NSMutableArray *dataSource;
@property(nonatomic,strong) MemberManagementHeaderView *tableHeaderView;
@property(nonatomic,strong) MemberModel *adminModel;

@end

@implementation MemberManagementViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self updateUserList];

}

#pragma mark --- 点击事件
- (void)onRightButtonClick:(id)sender
{
    AddMemberViewController *vc = [AddMemberViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)deleteUserWithID:(NSString *)userID
{
    [[AlertViewManager shareManager] initWithTitle:@"删除成员" message:@"确认是否要删除此成员，删除成员后他将不可控制该设备" cancleButtonTitle:@"取消" OtherButtonsArray:@[@"确定"] clickAtIndex:^(NSInteger buttonIndex, UIAlertController *alertController) {
        if (buttonIndex == 1)
        {
            DeleteUserApi *api = [[DeleteUserApi alloc] initWithDic:@{@"device_id":EXRACT_OBJECT(DEVICEID),@"del_uid":userID}];
            [api dtStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
                [SVProgressHUD showSuccessWithStatus:[request responseJSONObject][@"errmsg"]];
                [self updateUserList];
            } failure:nil];
        }
    }];
    

}


#pragma mark -- 事件处理
- (void)refreshBaseControlValue
{
    
}

- (void)updateUserList
{
    GetUserListApi *api = [[GetUserListApi alloc] initWithDic:@{@"sn":EXRACT_OBJECT(DEVICEMAC)}];
    [api dtStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
        NSDictionary *dict = [request responseJSONObject];
        [self.dataSource removeAllObjects];
        [self.dataSource addObjectsFromArray:[NSArray yy_modelArrayWithClass:[MemberModel class] json:dict[@"data"]]];
        for (MemberModel *model in self.dataSource) {
            if ([model.uid isEqualToString:dict[@"admin_uid"]])
            {
                self.adminModel = model;
            }
        }
        [self.dataSource removeObject:self.adminModel];
        _tableHeaderView.memberModel = self.adminModel;
        [self.tableView reloadData];
    } failure:nil];
}

#pragma mark --- tabelview代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return px1920Height(150);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MemberManagementTableViewCell *cell = [MemberManagementTableViewCell cellWithTableView:tableView forIndexPath:indexPath];
    MemberModel *model = self.dataSource[indexPath.row];
    cell.memberModel = model;
    if ([model.uid isEqualToString:self.adminModel.uid])
    {
        cell.deleteButton.hidden = YES;
    }
//    [cell setName:model.nickname phone:model.username];
    __weak typeof(self) weakSelf = self;
    cell.deleteBlock = ^{
        [weakSelf deleteUserWithID:model.ids];
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark --- 懒加载
- (UITableView *)tableView
{
    if(!_tableView)
    {
        UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        [superView addSubview:_tableView];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableHeaderView = self.tableHeaderView;
    }
    return _tableView;
}



- (MemberManagementHeaderView *)tableHeaderView
{
    if (!_tableHeaderView)
    {
        UIView *superView = self.tableView;
        _tableHeaderView = [[MemberManagementHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, px1920Height(600))];
        [superView addSubview:_tableHeaderView];
        _tableHeaderView.iconImageView.image = imageWithName(@"head");
//        [_tableHeaderView setName:@"爸爸" phone:@"13715881560"];
    }
    return _tableHeaderView;
}

- (NSMutableArray *)dataSource
{
    if(!_dataSource)
    {
        _dataSource = [[NSMutableArray alloc]init];
    }
    return _dataSource;
}



- (void)masLayoutSubview
{
    __weak UIView *superView = self.view;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(64);
        make.left.mas_equalTo(superView.mas_left).with.offset(px1080Width(40));
        make.right.mas_equalTo(superView.mas_right).with.offset(-px1080Width(40));
        make.bottom.mas_equalTo(superView.mas_bottom);
    }];
    
    
}


- (void)setNavigationStyle
{
    [self setNavigationBarTitle:@"成员管理"];
    [self setRightButtonImage:imageWithName(@"nav_add")];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
